const app = getApp();
Component({
  properties: {
    score:{
      type: Number,
      value: 0,
      observer(score){
        this.calcStar(score)
      }
    },
    disabled: {
      type: Boolean,
      value: false
    }
  },
  data: {
    starArr:[-1,-1,-1,-1,-1]//1是满星，0是半星，其他是空星
  },
  methods: {
    onStarTap(e) {
      if (this.properties.disabled) {
        return;
      }
      const selectScore = Number(e.currentTarget.dataset.index); 
      this.setData({ score: selectScore }); 
      this.calcStar(selectScore); 
    },
    calcStar(score){
      var starArr = []
      var integeNumber = Math.floor(score)
      var decimalNumber = score - integeNumber
      for(var index = 0; index < integeNumber; index++){
        starArr.push(1)
      }
      if(decimalNumber){
        starArr.push(0)
      }
      if(score>=5){
        score = 5
      }
      this.setData({
        starArr:starArr,
        score:score
      });
      this.triggerEvent('scorechange', { score: score });
    }
  }
})